import httputil.httpclientauth.RequestKerberosUrlUtils;
import httputil.httpclientauth.RequestKerberosUrlUtils2;
import httputil.okhttpinterceptor.HTTPKerberosAuthInterceptor;
import httputil.okhttpinterceptor.KerberosUtils;
import okhttp3.*;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Created by root on 20180118.
 * Update date:
 * <p>
 * Time: 11:34 AM
 * Project: test-java
 * Package: com.test.java.Thread
 * Describe :
 * <p>
 * Result of Test: test ok,test error
 * Command:
 * <p>
 * <p>
 * Email:  jifei.yang@ngaa.com.cn
 * Status：Using online
 * Attention：
 */
public class MonitSparkJobsOkHttp {
    private static Logger logger= LoggerFactory.getLogger(MonitSparkJobsOkHttp.class);



    public static void main(String[] args) {
        String keberosuser ="bigdata-app-ecpproddscat-ecpproddscat@ECOM.TLRG.COM";
        String keytab="C:\\Work\\DevTasks\\Code\\mathutil\\conf\\clusters\\Fornax\\hbase.keytab";
        String krb5Location="C:\\Work\\DevTasks\\Code\\mathutil\\conf\\clusters\\Fornax\\krb5.ini";
        System.setProperty("java.security.krb5.conf", krb5Location);
        if (true) {
            System.setProperty("sun.security.spnego.debug", "true");
            System.setProperty("sun.security.krb5.debug", "true");
        }
        String host = "c286ghr.ecom.bigdata.int.thomsonreuters.com";

        Map<String,String> kerberosOptions = new HashMap<String, String>();
        kerberosOptions.put("useTicketCache", "false");
        kerberosOptions.put("useKeyTab", "true");
        kerberosOptions.put("keyTab", keytab);
        //Krb5 in GSS API needs to be refreshed so it does not throw the error
        //Specified version of key is not available
        kerberosOptions.put("refreshKrb5Config", "true");
        kerberosOptions.put("principal", keberosuser);
        kerberosOptions.put("storeKey", "true");
        kerberosOptions.put("doNotPrompt", "true");
        kerberosOptions.put("isInitiator", "true");
        kerberosOptions.put("debug", "true");


        try{



            // refer to https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Open_and_Read_a_File
            // list dir
            String url_liststatus="http://c286ghr.ecom.bigdata.int.thomsonreuters.com:8088/ws/v1/cluster/apps";
            String url_listapp=   "http://c286ghr.ecom.bigdata.int.thomsonreuters.com:8088/ws/v1/cluster/apps/application_1565648131618_14328";
            HttpUrl.Builder urlBuilder = HttpUrl.parse(url_listapp)
                                                .newBuilder();

            // urlBuilder.addQueryParameter("queue", "root.ecpproddscat");
            // urlBuilder.addQueryParameter("user", "bigdata-app-ecpproddscat-ecpproddscat");
            //urlBuilder.addQueryParameter("states", "RUNNING");
           // urlBuilder.addQueryParameter("applicationTags", "searchstreaming");

            //User-Agent must be Curl or Java
            Request request = new Request.Builder()
                    .url(urlBuilder.build())
                    .addHeader("User-Agent","Java")
                    .addHeader("Accept-Encoding","zip,deflate")
                    .get()
                    .build();

            KerberosUtils ku = new KerberosUtils(host,kerberosOptions);
            ku.executeRequest( request);

            //RequestKerberosUrlUtils2 restTest = new RequestKerberosUrlUtils2(keberosuser,keytab,krb5Location, true);
            //restTest.callRestRequest(request,keberosuser);
            /*
             * 添加请求头信息
             */
            // 浏览器表示
            //httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
            // 传输的类型
            //httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");




            //System.out.println("string：\n"+new String(IOUtils.toByteArray(is), "UTF-8"));

        }catch (Exception exp){
            exp.printStackTrace();
        }

    }
}
